﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Business_Layer
{
   public class Utility
    {
       //Xuất danh sách ra file excel
       public void ExportToExcel(DataGridView dgv,string _path)
       {
           try
           {
               // tạo ứng dụng Excel
               Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
               // Tạo WorkBook mới 
               Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
               // tạo Sheet nới 
               Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
               app.Visible = true;
               //Khai báo Sheet đầu tiên để làm việc
               worksheet = workbook.Sheets["Sheet1"];
               worksheet = workbook.ActiveSheet;
               // Thay đổi tên Sheet
               worksheet.Name = "Exported from gridview";
               // Lưu trữ dữ liệu cho dòng header
               for (int i = 2; i < dgv.Columns.Count + 1; i++)
                   worksheet.Cells[1, i] = dgv.Columns[i - 1].HeaderText;

               // Lưu trữ dữ liệu cho các dòng, cột 
               for (int i = 0; i < dgv.Rows.Count - 1; i++)
                   for (int j = 1; j < dgv.Columns.Count; j++)
                   {
                       worksheet.Cells[i + 2, j + 1] = dgv.Rows[i].Cells[j].Value.ToString();
                   }

               // Lưu file 
               workbook.SaveAs(_path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
               // Thoát khỏi ứng dụng Excel                   
               app.Quit();


           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }


       //Xuất danh sách ra file PDF
       public void ExportToPDF(DataGridView dgv, string _path)
       {
           {
               try
               {
                   Document doc = new Document(PageSize.A4);
                   PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(_path, FileMode.Create));
                   doc.Open();
                   PdfPTable table = new PdfPTable(dgv.Columns.Count-1);
                   int column = dgv.Columns.Count;
                   int[] mang = new int[column-1];
                    const string FONT = "c:/windows/fonts/arialbd.ttf";
                    BaseFont bf = BaseFont.CreateFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                   int dem = 0;
                   for (int i = 1; i < column; i++)
                   {
                       mang[dem] = dgv.Columns[i].Width;
                       dem++;
                   }
                   table.SetWidths(mang);
                   for (int i = 1; i < dgv.Columns.Count; i++)
                   {
                       table.AddCell(new Phrase(dgv.Columns[i].HeaderText,
                                                new Font(bf,10,Font.BOLD)));
                   }
                   table.HeaderRows = 1;    
                   for (int i = 0; i < dgv.Rows.Count; i++)
                   {
                       for (int j = 1; j < dgv.Columns.Count; j++)
                       {
                           if (dgv[j, i].Value != null)
                               table.AddCell(new Phrase(dgv[j, i].Value.ToString(),new Font(bf,9,Font.NORMAL)));
						   else
							   table.AddCell("");
                       }
                   }
                   doc.Add(table);
                   doc.Close();
                   if (MessageBox.Show("Export successful, you want to open it?", "Notice", MessageBoxButtons.YesNo,
                                       MessageBoxIcon.Question) == DialogResult.Yes)
                   {
                       System.Diagnostics.Process.Start("\"" + _path + "\"");
                   }
               }
               catch (Exception ex)
               {
                   MessageBox.Show("Error: \n" + ex.Message);
               }

           }
       }


       //Mã hóa MD5
       public  byte[] encryptData(string data)
       {
           System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
           byte[] hashedBytes;
           System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
           hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(data));
           return hashedBytes;
       }
       public  string md5(string data)
       {
           return BitConverter.ToString(encryptData(data)).Replace("-", "").ToLower();
       }
     



    }
}
